package com.wicket.okrbff.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.wicket.okrbff.biz.service.RoleService;
import com.wicket.okrbff.biz.service.dto.req.BatchClearUserInfoFromRedisReqDto;
import com.wicket.okrbff.biz.service.dto.req.*;
import com.wicket.okrbff.biz.service.dto.res.BatchClearUserInfoFromRedisRespDto;
import com.wicket.okrbff.biz.service.dto.res.*;
import com.wicket.okrbff.biz.service.nb.NbRole;
import com.wicket.okrbff.common.util.CommonFunctionHelper;
import com.wicket.okrbff.common.util.RedisUtil;
import com.wicket.okrframework.integration.FwBaseDivineDataClient;
import com.wicket.okrframework.integration.FwBaseInductionRecordClient;
import com.wicket.okrframework.integration.FwBaseMemberClient;
import com.wicket.okrframework.integration.FwBaseRoleClient;
import com.wicket.okrframework.integration.dto.*;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.stream.Collectors;
import com.wicket.okrbff.common.exception.Assert;

/**
 * @author ：
 * @date ：
 *
 *
 * @version 1.0
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    FwBaseRoleClient fwBaseRoleClient;
    @Resource
    FwBaseMemberClient fwBaseMemberClient;
    @Resource
    FwBaseDivineDataClient fwBaseDivineDataClient;
    @Resource
    NbRole nbRole;
    @Resource
    FwBaseInductionRecordClient fwBaseInductionRecordClient;
    @Resource
    private RedisUtil redisUtil;




    /**
   * B1-2查询角色详情（管理）[1451]
   * gen by moon at 8/10/2022, 3:35:41 AM
   */
    @Trace(operationName = "B1-2查询角色详情（管理）")
    @Tags({@Tag(key = "参数",value = "arg[0]"),
            @Tag(key = "返回值",value = "returnedObj")})
  @Override
  public BffQueryRoleDetailRespDto queryRoleDetail(BffQueryRoleDetailReqDto reqDto){
      
       
       QueryRoleDetailRespDto queryRoleDetailRespDto_1 = null;
//步骤0: D1-2查询角色详情 - queryRoleDetail
     QueryRoleDetailRespDto queryRoleDetailRespDto = null;
    QueryRoleDetailReqDto queryRoleDetailReqDto=new QueryRoleDetailReqDto();
  queryRoleDetailReqDto.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:35075_1
queryRoleDetailReqDto.setPlatformData("TRUE");//sourceId:35076_1
if(reqDto!=null){
      queryRoleDetailReqDto.setRoleId(reqDto.getRoleId());//sourceId:35057_1
    }
  
    /*D1-2查询角色详情[1163]   */
    
      queryRoleDetailRespDto = fwBaseRoleClient.queryRoleDetail(queryRoleDetailReqDto).getData();
      
      queryRoleDetailRespDto_1 = queryRoleDetailRespDto;
    
BffQueryRoleDetailRespDto retData = new BffQueryRoleDetailRespDto();
  if(queryRoleDetailRespDto_1!=null){
      retData.setRoleId(queryRoleDetailRespDto_1.getRoleId());//sourceId:35088_1
retData.setRoleCode(queryRoleDetailRespDto_1.getRoleCode());//sourceId:35089_1
retData.setRoleName(queryRoleDetailRespDto_1.getRoleName());//sourceId:35090_1
retData.setRoleRemark(queryRoleDetailRespDto_1.getRoleRemark());//sourceId:35091_1
retData.setRoleLogo(queryRoleDetailRespDto_1.getRoleLogo());//sourceId:35092_1
retData.setIsFreeze(queryRoleDetailRespDto_1.getIsFreeze());//sourceId:35105_1
    }
  

  
  
return retData;
  }

    /**
   * B1-2查询自定义角色列表（管理）[1450]
   * gen by moon at 8/10/2022, 3:35:38 AM
   */
    @Trace(operationName = "B1-2查询自定义角色列表（管理）")
    @Tags({@Tag(key = "参数",value = "arg[0]"),
            @Tag(key = "返回值",value = "returnedObj")})
  @Override
  public BffQueryCustomRoleListRespDto queryCustomRoleList(BffQueryCustomRoleListReqDto reqDto){
      
       
       QueryRoleListRespDto queryRoleListRespDto_1 = null;
//步骤0: D1-2查询角色列表 - queryRoleList
     QueryRoleListRespDto queryRoleListRespDto = null;
    QueryRoleListReqDto queryRoleListReqDto=new QueryRoleListReqDto();
  queryRoleListReqDto.setRoleType("BUSINESS_ROLE");//sourceId:34986_1
queryRoleListReqDto.setPlatformData("FALSE");//sourceId:34989_1
queryRoleListReqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID//sourceId:34998_1
if(reqDto!=null){
      queryRoleListReqDto.setIsArchive(reqDto.getIsArchive());//sourceId:34991_1
    }
  
    /*D1-2查询角色列表[543]   */
    Assert.isNull(queryRoleListReqDto.getRoleType(),"B1-2查询自定义角色列表（管理）-D1-2查询角色列表-角色类型不能为空",false);
Assert.isNull(queryRoleListReqDto.getPlatformData(),"B1-2查询自定义角色列表（管理）-D1-2查询角色列表-是否平台数据不能为空",false);
Assert.isNull(queryRoleListReqDto.getIsArchive(),"B1-2查询自定义角色列表（管理）-D1-2查询角色列表-是否存档不能为空",false);
Assert.isNull(queryRoleListReqDto.getSpaceId(),"B1-2查询自定义角色列表（管理）-D1-2查询角色列表-创建于空间ID不能为空",false);
      queryRoleListRespDto = fwBaseRoleClient.queryRoleList(queryRoleListReqDto).getData();
      
      queryRoleListRespDto_1 = queryRoleListRespDto;
    
BffQueryCustomRoleListRespDto retData = new BffQueryCustomRoleListRespDto();
  if(queryRoleListRespDto_1!=null){
      retData.setRoleList(queryRoleListRespDto_1.getRoleList().stream().map(item -> BeanUtil.toBean(item, BffFrameworkSceneRoleDto.class)).collect(Collectors.toList()));//sourceId:35000_1
    }
  

  retData.setPageNum(queryRoleListRespDto.getPageNum());
        retData.setPageSize(queryRoleListRespDto.getPageSize());
        retData.setTotal(queryRoleListRespDto.getTotalNum());
        retData.setIsLastPage((Math.ceil(Double.valueOf(queryRoleListRespDto.getTotalNum()) / Double.valueOf(queryRoleListRespDto.getPageSize()))) <= retData.getPageNum());
        
  
return retData;
  }

    /**
   * B1-2查询自定义角色详情（管理）[1056]
   * gen by moon at 8/10/2022, 3:32:04 AM
   */
    @Trace(operationName = "B1-2查询自定义角色详情（管理）")
    @Tags({@Tag(key = "参数",value = "arg[0]"),
            @Tag(key = "返回值",value = "returnedObj")})
  @Override
  public BffQueryCustomRoleDetailRespDto queryCustomRoleDetail(BffQueryCustomRoleDetailReqDto reqDto){
      
       
       QueryRoleDetailRespDto queryRoleDetailRespDto_1 = null;
QueryInductionRecordInfoDetailComRespDto queryInductionRecordInfoDetailComRespDto_1 = null;
//步骤0: D1-2查询角色详情 - queryRoleDetail
     QueryRoleDetailRespDto queryRoleDetailRespDto = null;
    QueryRoleDetailReqDto queryRoleDetailReqDto=new QueryRoleDetailReqDto();
  if(reqDto!=null){
      queryRoleDetailReqDto.setRoleId(reqDto.getRoleId());//sourceId:25115_1
    }
  
    /*D1-2查询角色详情[1163]   */
    
      queryRoleDetailRespDto = fwBaseRoleClient.queryRoleDetail(queryRoleDetailReqDto).getData();
      
      queryRoleDetailRespDto_1 = queryRoleDetailRespDto;
    
//步骤1: D1-3查询个人就职记录信息详情（公共） - queryInductionRecordInfoDetailCom
     QueryInductionRecordInfoDetailComRespDto queryInductionRecordInfoDetailComRespDto = null;
    if(queryRoleDetailRespDto !=null){
          QueryInductionRecordInfoDetailComReqDto queryInductionRecordInfoDetailComReqDto=new QueryInductionRecordInfoDetailComReqDto();
  if(queryRoleDetailRespDto!=null){
      queryInductionRecordInfoDetailComReqDto.setInductionRecordId(queryRoleDetailRespDto.getOperationInductionId());//sourceId:24318_1
    }
  
    /*D1-3查询个人就职记录信息详情（公共）[715]   */
    Assert.isNull(queryInductionRecordInfoDetailComReqDto.getInductionRecordId(),"B1-2查询自定义角色详情（管理）-D1-3查询个人就职记录信息详情（公共）-就职记录ID不能为空",false);
      queryInductionRecordInfoDetailComRespDto = fwBaseInductionRecordClient.queryInductionRecordInfoDetailCom(queryInductionRecordInfoDetailComReqDto).getData();
      
      queryInductionRecordInfoDetailComRespDto_1 = queryInductionRecordInfoDetailComRespDto;
           }
    
BffQueryCustomRoleDetailRespDto retData = new BffQueryCustomRoleDetailRespDto();
  if(queryRoleDetailRespDto_1!=null){
      retData.setRoleId(queryRoleDetailRespDto_1.getRoleId());//sourceId:25141_1
retData.setRoleCode(queryRoleDetailRespDto_1.getRoleCode());//sourceId:25142_1
retData.setRoleName(queryRoleDetailRespDto_1.getRoleName());//sourceId:25145_1
retData.setRoleLogo(queryRoleDetailRespDto_1.getRoleLogo());//sourceId:25149_1
retData.setRoleRemark(queryRoleDetailRespDto_1.getRoleRemark());//sourceId:25147_1
retData.setIsFreeze(queryRoleDetailRespDto_1.getIsFreeze());//sourceId:35107_1
retData.setTempFreezeStatus(queryRoleDetailRespDto_1.getTempFreezeStatus());//sourceId:35022_1
retData.setSubjectLifeCycle(queryRoleDetailRespDto_1.getSubjectLifeCycle());//sourceId:35023_1
retData.setOperationInductionId(queryRoleDetailRespDto_1.getOperationInductionId());//sourceId:34718_1
retData.setOperateTime(queryRoleDetailRespDto_1.getOperateTime());//sourceId:25160_1
    }
if(queryInductionRecordInfoDetailComRespDto_1!=null){
      retData.setUserId(queryInductionRecordInfoDetailComRespDto_1.getUserId());//sourceId:25144_1
retData.setUserFace(queryInductionRecordInfoDetailComRespDto_1.getUserFace());//sourceId:25150_1
retData.setIdCardName(queryInductionRecordInfoDetailComRespDto_1.getIdCardName());//sourceId:25146_1
retData.setNickName(queryInductionRecordInfoDetailComRespDto_1.getNickName());//sourceId:35032_1
    }
  

  
  
return retData;
  }

    /**
   * B1-2修改自定义角色（管理）[634]
   * gen by moon at 8/10/2022, 3:29:43 AM
   */
    @Trace(operationName = "B1-2修改自定义角色（管理）")
    @Tags({@Tag(key = "参数",value = "arg[0]"),
            @Tag(key = "返回值",value = "returnedObj")})
  @Override
  public BffUpdateCustomRoleRespDto updateCustomRole(BffUpdateCustomRoleReqDto reqDto){
      
       
       //步骤0: D1-2修改自定义角色 - updateCustomRole
     UpdateCustomRoleRespDto updateCustomRoleRespDto = null;
    UpdateCustomRoleReqDto updateCustomRoleReqDto=new UpdateCustomRoleReqDto();
  if(reqDto!=null){
      updateCustomRoleReqDto.setRoleId(reqDto.getRoleId());//sourceId:34124_1
updateCustomRoleReqDto.setRoleRemark(reqDto.getRoleRemark());//sourceId:34125_1
updateCustomRoleReqDto.setRoleLogo(reqDto.getRoleLogo());//sourceId:34126_1
    }
  
    /*D1-2修改自定义角色[640]   */
    Assert.isNull(updateCustomRoleReqDto.getRoleId(),"B1-2修改自定义角色（管理）-D1-2修改自定义角色-角色ID不能为空",false);
      updateCustomRoleRespDto = fwBaseRoleClient.updateCustomRole(updateCustomRoleReqDto).getData();
      
      
    
//步骤1: D1-2查询角色人员列表 - queryRoleRelList
     QueryRoleRelListRespDto queryRoleRelListRespDto = null;
    QueryRoleRelListReqDto queryRoleRelListReqDto=new QueryRoleRelListReqDto();
  if(reqDto!=null){
      queryRoleRelListReqDto.setRoleId(reqDto.getRoleId());//sourceId:53672_1
    }
  
    /*D1-2查询角色人员列表[1134]   */
    
      queryRoleRelListRespDto = fwBaseMemberClient.queryRoleRelList(queryRoleRelListReqDto).getData();
      
      
    
//步骤2: Redis批量清空用户信息 - batchClearUserInfoFromRedis
     //ModelCode: batchClearUserInfoFromRedis
        BatchClearUserInfoFromRedisRespDto batchClearUserInfoFromRedisRes = null;
    if(queryRoleRelListRespDto !=null){
          BatchClearUserInfoFromRedisReqDto batchClearUserInfoFromRedisReq=new BatchClearUserInfoFromRedisReqDto();
  if(queryRoleRelListRespDto!=null){
      batchClearUserInfoFromRedisReq.setBeginMemberList(queryRoleRelListRespDto.getRoleRelList().stream().map(item->item.getOriginalRoleMemberId())
        .collect(Collectors.toList()));//sourceId:53674_1
    }
  
    /*Redis批量清空用户信息[1713]  入参身份ID数据集，清除用户就职信息缓存 */
    
      batchClearUserInfoFromRedisRes = nbRole.batchClearUserInfoFromRedis(batchClearUserInfoFromRedisReq);
      
      
           }
    
BffUpdateCustomRoleRespDto retData = new BffUpdateCustomRoleRespDto();
  
  

  
  
return retData;
  }





    /**
   * B1-2存档自定义角色（管理）[1721]
   * gen by moon at 8/10/2022, 3:37:16 AM
   */
    @Trace(operationName = "B1-2存档自定义角色（管理）")
    @Tags({@Tag(key = "参数",value = "arg[0]"),
            @Tag(key = "返回值",value = "returnedObj")})
  @Override
  public BffArchiveCustomRoleRespDto archiveCustomRole(BffArchiveCustomRoleReqDto reqDto){
      
       
       //步骤0: D1-2存档自定义角色 - archiveCustomRole
     ArchiveCustomRoleRespDto archiveCustomRoleRespDto = null;
    ArchiveCustomRoleReqDto archiveCustomRoleReqDto=new ArchiveCustomRoleReqDto();
  if(reqDto!=null){
      archiveCustomRoleReqDto.setRoleId(reqDto.getRoleId());//sourceId:51857_1
    }
  
    /*D1-2存档自定义角色[1722]   */
    Assert.isNull(archiveCustomRoleReqDto.getRoleId(),"B1-2存档自定义角色（管理）-D1-2存档自定义角色-角色ID不能为空",false);
      archiveCustomRoleRespDto = fwBaseRoleClient.archiveCustomRole(archiveCustomRoleReqDto).getData();
      
      
    
//步骤1: D1-2查询角色人员列表 - queryRoleRelList
     QueryRoleRelListRespDto queryRoleRelListRespDto = null;
    QueryRoleRelListReqDto queryRoleRelListReqDto=new QueryRoleRelListReqDto();
  if(reqDto!=null){
      queryRoleRelListReqDto.setRoleId(reqDto.getRoleId());//sourceId:51866_1
    }
  
    /*D1-2查询角色人员列表[1134]   */
    Assert.isNull(queryRoleRelListReqDto.getRoleId(),"B1-2存档自定义角色（管理）-D1-2查询角色人员列表-角色ID不能为空",false);
      queryRoleRelListRespDto = fwBaseMemberClient.queryRoleRelList(queryRoleRelListReqDto).getData();
      
      
    
//步骤2: Redis批量清空用户信息 - batchClearUserInfoFromRedis
     //ModelCode: batchClearUserInfoFromRedis
        BatchClearUserInfoFromRedisRespDto batchClearUserInfoFromRedisRes = null;
    if(queryRoleRelListRespDto !=null){
          BatchClearUserInfoFromRedisReqDto batchClearUserInfoFromRedisReq=new BatchClearUserInfoFromRedisReqDto();
  if(queryRoleRelListRespDto!=null){
      batchClearUserInfoFromRedisReq.setBeginMemberList(queryRoleRelListRespDto.getRoleRelList().stream().map(item->item.getOriginalRoleMemberId())
        .collect(Collectors.toList()));//sourceId:51868_1
    }
  
    /*Redis批量清空用户信息[1713]  入参身份ID数据集，清除用户就职信息缓存 */
    
      batchClearUserInfoFromRedisRes = nbRole.batchClearUserInfoFromRedis(batchClearUserInfoFromRedisReq);
      
      
           }
    
BffArchiveCustomRoleRespDto retData = new BffArchiveCustomRoleRespDto();
  
  

  
  
return retData;
  }
    //
}
